
import LXCOM from '../common/common.js'
import UtilFuncs from '../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;

import {LGXEnums}  from '../common/sysenum.js';


var RotateGraphSvc = {}

RotateGraphSvc.rotate = function (rotCnt, angleDegre, graphlist, mapscale) {
    let angle = angleDegre * Math.PI / 180.0;
    graphlist.forEach(graph => {
            this.rotateSingleGraph(rotCnt, angle, graph, mapscale);
    });
}

RotateGraphSvc.rotateSingleGraph = function (rotCnt, angle, graph, mapscale) {
    let graphPreAngle = graph.getRadAngle();
    let graphCnt = graph.getLocalOrigin();
    let x = graphCnt.x;
    let y = graphCnt.y;
    let devOrgPt = { x, y };
    var devOrgRotPt = utilfuncs.rotate(devOrgPt, rotCnt, angle);

    let axisLen = 10;
    x = graphCnt.x + axisLen * Math.cos(graphPreAngle);
    y = graphCnt.y + axisLen * Math.sin(graphPreAngle);
    let devAxisXPt = { x, y };
    var devAxisXRotPt = utilfuncs.rotate(devAxisXPt, rotCnt, angle);

    let vct = new LXCOM.LGXVector();
    vct.setBy2Pt(devOrgRotPt, devAxisXRotPt);
    let revolveWorldAngle = vct.angleFrmXAxisPositiveDir();
    graph.setAngle(revolveWorldAngle);
    graph.rotateCoord(rotCnt, angle);
    if (graph.getPosType() == LGXEnums.LGXPosMode.point_mode) {
        x = devOrgRotPt.x - graphCnt.x;
        y = devOrgRotPt.y - graphCnt.y;
        let offsetPt = { x, y };
        graph.translate(offsetPt, mapscale,false);
    }

    let topoRes = graph.getTopoRes();
    if (graph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type && topoRes){
        let nodeList = topoRes.getConnNodeList();
        for (let i = 0;i < nodeList.length;i++){
            let node = nodeList[i];
            let nodePt = node.getPos();
            var newPt = utilfuncs.rotate(nodePt, rotCnt, angle);
            node.setPos(newPt.x,newPt.y);
        }
    }

    graph.calScope(mapscale);
}
export default { RotateGraphSvc }